﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NCMIS.Extensions
{
    /// <summary>
    /// Utility class for manipulating extensions. Used by the RequestParameter class.
    /// </summary>
    public static class ConvertExtensions
    {
        public static Nullable<bool> ToNullableBoolean(this String value)
        {
            Nullable<bool> result = null;
            bool tempValue;
            if (Boolean.TryParse(value, out tempValue))
                result = tempValue;

            return result;
        }

        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }

        public static string ToPascalCase(this string str)
        {
            //
            // Modified version of http://www.dreamincode.net/code/snippet2490.htm.
            //

            if (str == null) throw new ArgumentNullException("str", "Null is not a valid string.");

            if (str.Length == 0) return str;
            string[] words = str.Split(' ');

             for (int i = 0; i < words.Length; i++)
            {
                if (words[i].Length > 0)
                {
                    string word = words[i];
                    char firstLetter = char.ToUpper(word[0]);
                    words[i] = firstLetter + word.Substring(1);
                }
            }

            return string.Join(string.Empty, words);
        } 
    }
}
